package com.awaken.content.service.dubbo.impl;

import com.awaken.content.models.dto.UserDTO;
import com.awaken.content.models.entity.User;
import com.awaken.content.models.request.UserRequest;
import com.awaken.content.mapper.UserMapper;
import com.awaken.content.service.dubbo.UserDubboService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户Dubbo服务实现类
 *
 * @author awaken-lin
 */
@Slf4j
@Service
public class UserDubboServiceImpl extends ServiceImpl<UserMapper, User> implements UserDubboService {

    @Override
    public UserDTO register(UserRequest request) {
        log.info("用户注册: {}", request.getUsername());
        
        // 检查用户名是否已存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", request.getUsername());
        User existingUser = this.getOne(queryWrapper);
        if (existingUser != null) {
            log.warn("用户名已存在: {}", request.getUsername());
            return null;
        }
        
        // 创建新用户
        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(request.getPassword());
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());
        
        this.save(user);
        
        // 转换为DTO
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        
        log.info("用户注册成功: {}", userDTO);
        return userDTO;
    }

    @Override
    public UserDTO login(UserRequest request) {
        log.info("用户登录: {}", request.getUsername());
        
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", request.getUsername());
        User user = this.getOne(queryWrapper);
        
        if (user == null) {
            log.warn("用户不存在: {}", request.getUsername());
            return null;
        }
        
        if (!user.getPassword().equals(request.getPassword())) {
            log.warn("密码错误: {}", request.getUsername());
            return null;
        }
        
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        
        log.info("用户登录成功: {}", userDTO);
        return userDTO;
    }

    @Override
    public UserDTO getUserById(Long id) {
        log.info("获取用户信息: {}", id);
        
        User user = this.getById(id);
        if (user == null) {
            log.warn("用户不存在: {}", id);
            return null;
        }
        
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }

    @Override
    public List<UserDTO> getAllUsers() {
        log.info("获取所有用户列表");
        
        List<User> users = this.list();
        return users.stream()
                .map(user -> {
                    UserDTO userDTO = new UserDTO();
                    BeanUtils.copyProperties(user, userDTO);
                    return userDTO;
                })
                .collect(Collectors.toList());
    }
} 